package com.example.hdwx.myapplication.json;

import android.util.Log;

import com.example.hdwx.myapplication.eneity.daydaycook_eneitys.Daydaycook_eneity;
import com.example.hdwx.myapplication.eneity.daydaycook_eneitys.Daydaycook_everyday_eneity;
import com.example.hdwx.myapplication.eneity.daydaycook_eneitys.Daydaycook_hot_eneity;
import com.example.hdwx.myapplication.eneity.daydaycook_eneitys.Daydaycook_raiders_eneity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hdwx on 2017/2/13.
 */

public class Daydaycook_json {
    final  String TAG="Daydaycook_json";
    //图片轮播json数据
    public List<Daydaycook_eneity> image_json(String s) {
        try {
            List<Daydaycook_eneity> list = new ArrayList();

            JSONObject object = new JSONObject(s);
            JSONArray array = object.getJSONArray("data");
            for (int i = 0; i < array.length(); i++) {
                Daydaycook_eneity eneity = new Daydaycook_eneity();
                eneity.setImg_url(array.getJSONObject(i).getString("path"));
                eneity.setImg_title(array.getJSONObject(i).getString("title"));
                eneity.setId(array.getJSONObject(i).getString("id"));
                eneity.setUrl_id(array.getJSONObject(i).getString("url"));
                eneity.save();
                if(eneity.save()){
                    Log.e(TAG,"Daydaycook_eneity数据存储成功");
                }else {
                    Log.e(TAG,"Daydaycook_eneity数据存储失败");
                }
                list.add(eneity);
            }
            Log.e("1111", list.get(2).getImg_title() + "" + list.size());
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    //每日新菜馆json数据
   public List<Daydaycook_everyday_eneity> everyday_json(String s){
       List list=new ArrayList();
       try {
           JSONObject object = new JSONObject(s);
           JSONArray array = object.getJSONArray("data");
           for (int i = 0; i < array.length(); i++) {
               Daydaycook_everyday_eneity eneity = new Daydaycook_everyday_eneity();
               eneity.setName(array.getJSONObject(i).getString("name"));
               eneity.setImg_url(array.getJSONObject(i).getString("imageUrl"));
               eneity.setDescription(array.getJSONObject(i).getString("description"));
               eneity.setNum(array.getJSONObject(i).getString("clickCount"));
               eneity.setVideo_url(array.getJSONObject(i).getString("detailsUrl"));
               eneity.save();
               if(eneity.save()){
                   Log.e(TAG,"Daydaycook_everyday_eneity数据存储成功");
               }else {
                   Log.e(TAG,"Daydaycook_everyday_eneity数据存储失败");
               }
               list.add(eneity);
           }

           // Log.e("1111", list.get(2).getImg_title() + "" + list.size());
           return list;
       } catch (JSONException e) {
           e.printStackTrace();
       }
       return list;
   }
    //当红人气菜json数据
    public List<Daydaycook_hot_eneity> hot_json(String s){
        List<Daydaycook_hot_eneity> hot_list=new ArrayList();
        try {
            JSONObject object = new JSONObject(s);
            JSONArray array = object.getJSONArray("recipeList");
            Log.e("1111", "" + array.length());
            for (int i = 0; i < array.length(); i++) {
                if(!"".equals(array.getJSONObject(i).getString("description"))){
                Daydaycook_hot_eneity eneity1 = new Daydaycook_hot_eneity();
                eneity1.setName(array.getJSONObject(i).getString("title"));
                eneity1.setImg_url(array.getJSONObject(i).getString("image_url"));
                Log.e("1111", "" + array.getJSONObject(i).getString("title"));
                eneity1.setDescription(array.getJSONObject(i).getString("description"));
                eneity1.setNum(array.getJSONObject(i).getString("click_count"));
                    eneity1.save();
                    if(eneity1.save()){
                        Log.e(TAG,"Daydaycook_hot_eneity数据存储成功");
                    }else {
                        Log.e(TAG,"Daydaycook_hot_eneity数据存储失败");
                    }
                hot_list.add(eneity1);}
            }
            return hot_list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return hot_list;
    }
    //美食全攻略
    public List<Daydaycook_raiders_eneity> raiders_json(String s){
        List<Daydaycook_raiders_eneity> raiders_list=new ArrayList();
        try {
            JSONObject object = new JSONObject(s);
            JSONArray array = object.getJSONArray("themeList");
            for (int i = 0; i < array.length(); i++) {
                if(!"".equals(array.getJSONObject(i).getString("description"))){
                    Daydaycook_raiders_eneity eneity1 = new Daydaycook_raiders_eneity();
                    eneity1.setTitle(array.getJSONObject(i).getString("title"));
                    eneity1.setImg_url(array.getJSONObject(i).getString("image_url"));

                    eneity1.setDescription(array.getJSONObject(i).getString("description"));
                    Log.e("999",array.getJSONObject(i).get("source")+""+array.length());
                    if(array.getJSONObject(i).get("source").equals(null)){
                        eneity1.setDisplayAuthor("false");

                    }  else
                    {
                        eneity1.setDisplayAuthor(array.getJSONObject(i).getJSONObject("source").getString("displayAuthor"));
                    }
                    Log.e("1111", "" + array.getJSONObject(i).getString("title")+eneity1.getDisplayAuthor());
                    eneity1.save();
                    if(eneity1.save()){
                        Log.e(TAG,"Daydaycook_raiders_eneity数据存储成功");
                    }else {
                        Log.e(TAG,"Daydaycook_raiders_eneity数据存储失败");
                    }
                    raiders_list.add(eneity1);}
            }
            return raiders_list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return raiders_list;
    }

}
